const PromiseAll = (iterator) => {
  const promises = Array.from(iterator); // 对传入的数据进行浅拷贝，确保有遍历器
  const len = promises.length; // 长度
  let index = 0; // 每次执行成功+1,当等于长度时，说明所有数据都返回，则可以resolve
  let data = []; // 用来存放返回的数据数组
  return new Promise((resolve, reject) => {
    for (let i in promises) {
      promises[i]
        .then((res) => {
          data[i] = res;
          if (++index === len) {
            resolve(data);
          }
        })
        .catch((err) => {
          reject(err);
        });
    }
  });
};

const promise1 = Promise.resolve("promise1");
const promise2 = new Promise(function (resolve, reject) {
  setTimeout(resolve, 2000, "promise2");
});
const promise3 = new Promise(function (resolve, reject) {
  setTimeout(resolve, 1000, "promise3");
});

PromiseAll([promise1, promise2, promise3]).then(
  function (values) {
    console.log(values);
  },
  (err) => {
    console.log(err);
  }
);
